package com.zhiyou.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou.model.Homework;
import com.zhiyou.model.Homework_Notice;
import com.zhiyou.service.HomeworkService;
import com.zhiyou.utils.JsonResult;

@RestController
@RequestMapping("homework/")
public class HomeWorkController {

	@Autowired
	HomeworkService homeworkService;

	/**
	 * 根据学生id和时间查看所有科目作业通知 web,
	 * @param id
	 * @param time
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("listHN/{id}/{time}")
	public JsonResult selectByStu_Id(@PathVariable("id") Integer id, @PathVariable("time") String time,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Homework_Notice> list = homeworkService.selectByStu_Id(id, time);
			return new JsonResult(list, "根据学生id和时间查看所有科目作业通知成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}

	/**
	 * 老师根据科目与班级号发布作业通知
	 * @param homework_Notice
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "addHN")
	public JsonResult addHN(Homework_Notice homework_Notice,MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
		try {
			homeworkService.addHN(homework_Notice,file);
			return new JsonResult("老师根据科目与班级号发布作业通知 成功");
		} catch (Exception e) {
			return new JsonResult(500, "提交失败");
		}
	}

	/**
	 * 根据作业通知id查看作业通知详情
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("HN/{id}")
	public JsonResult selectByHN_id(@PathVariable("id") Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			
			Homework_Notice homework_Notice = homeworkService.selectByHN_id(id);
			return new JsonResult(homework_Notice, "根据作业通知id查看作业通知成功 ");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}
	/**
	 * 上传作业 
	 * @param homework
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "addH")
	public JsonResult addH(Homework homework,MultipartFile file ,HttpServletRequest request, HttpServletResponse response) {
		try {
			homeworkService.addH(homework,file);
			return new JsonResult("提交成功");
		} catch (Exception e) {
			return new JsonResult(500, "提交失败");
		}
	}
	/**
	 *  根据班级号与老师,日期,查看作业 	
	 * @param class_id
	 * @param teacher_id
	 * @param time
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("listH/{class_id}/{teacher_id}/{time}")
	public JsonResult selectAll(@PathVariable("class_id") Integer class_id,
			@PathVariable("teacher_id") Integer teacher_id, @PathVariable("time") String time,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Homework> list =  homeworkService.selectAll(class_id, teacher_id, time);
			return new JsonResult(list, "根据班级号与老师,日期,查看作业 成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}
	/**
	 * 根据作业id查看已提交作业的详情
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("H/{id}")
	public JsonResult selectByH_id(@PathVariable("id") Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Homework homework = homeworkService.selectByH_id(id);
			return new JsonResult(homework, "根据作业id查看已提交作业的详情");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}
	/**
	 * 批改作业 
	 * @param homework
	 * @param request
	 * @param response
	 * @return
	 */
	@PutMapping("update")
	public JsonResult update(Homework homework, HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println(homework);
			homeworkService.update(homework);;
			return new JsonResult("提交成功");
		} catch (Exception e) {
			return new JsonResult(500, "提交失败");
		}
	}
	/**
	 * 根据id删除作业通知
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@DeleteMapping("deleteHN/{id}")
	public JsonResult deleteHN(@PathVariable("id") Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			homeworkService.deleteHN(id);
			return new JsonResult("根据作业通知id删除作业通知成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}
	/**
	 * 根据id批量删除作业通知
	 * @param ids
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("deleteHNAll")
	public JsonResult deleteHN(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			homeworkService.deleteHNAll(ids);
			return new JsonResult("根据作业通知id批量删除作业通知成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}
	/**
	 * 根据id删除作业通知
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@DeleteMapping("deleteH/{id}")
	public JsonResult deleteH(@PathVariable("id") Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			homeworkService.deleteH(id);
			return new JsonResult("根据作业id删除作业成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}
	/**
	 * 根据id批量删除作业
	 * @param ids
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("deleteHAll")
	public JsonResult deleteH(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			homeworkService.deleteHAll(ids);
			return new JsonResult("根据作业id批量删除作业通知成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}
}
